import scrapy
import pymongo
from scrapy import cmdline
from scrapy.http import HtmlResponse


class QingTingSpider(scrapy.Spider):
    name = "qingting"
    allowed_domains = ["m.qingting.fm"]
    start_urls = ["http://m.qingting.fm/rank"]

    def parse(self, response: HtmlResponse, **kwargs):
        a_list = response.xpath('//div[@class="rank-list"]/a')
        for a_temp in a_list:
            # 如果没有使用extract方法返回的数据类型是一个Selector对象
            rank_number = a_temp.xpath('./div[@class="badge"]/text()').extract_first()  # 排名
            img_url = a_temp.xpath('./img/@src').extract_first()  # 图片地址
            title = a_temp.xpath('./div[@class="content"]/div[@class="title"]/text()').extract_first()  # 标题
            desc = a_temp.xpath('./div[@class="content"]/div[@class="desc"]/text()').extract_first()  # 描述
            play_number = a_temp.xpath('.//div[@class="info-item"][1]/span/text()').extract_first()

            print('---***--->', rank_number, img_url, title, desc, play_number)


if __name__ == '__main__':
    # 使用cmdline执行爬虫运行指令
    cmdline.execute('scrapy crawl qingting'.split())
